package zhishuang.wang.sansi.x9.devinfor;

import zhishuang.wang.sansi.tools.ReturnData;
import zhishuang.wang.sansi.tools.time.DateFormat;
import zhishuang.wang.sansi.x9.protocol.X9ProtocolDeal;
import zhishuang.wang.sansi.x9.tcpservice.DeviceRealStatus;
import zhishuang.wang.sansi.x9.tcpservice.NettyX9ServiceInit;

import java.util.*;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author Zhishuang.Wang
 * @Date 2023/7/5 11:24
 * @Email 1406110602@qq.com
 */

public class DeviceInforX9Var {
    public static int x9BlockSizeDeFault = 2048;
    public static ReentrantLock devX9InforMapLock = new ReentrantLock();
    public static Map<String, DeviceInforX9> devX9InforMap = new HashMap();
    public static Map<String, Integer> devX9ControlMap = new HashMap();

    public DeviceInforX9Var() {
    }

    public static ReturnData initX9DeviceInfor(String deviceId, String playId) {
        ReturnData rd = new ReturnData();
        DeviceInforX9 dix = new DeviceInforX9();
        dix.setFolNum(0);
        dix.setOnlineFlag(0);
        dix.setPlayIds(playId);
        devX9InforMapLock.lock();
        devX9InforMap.put(deviceId, dix);
        devX9InforMapLock.unlock();
        rd.setCode(0);
        rd.setMessage("添加设备信息成功");
        rd.setTime(DateFormat.getNowDataFormat());
        return rd;
    }

    public static ReturnData getX9DeviceOnlineStatus(String deviceId) {
        ReturnData rd = new ReturnData();
        rd.setCode(1);
        devX9InforMapLock.lock();
        DeviceInforX9 dix = (DeviceInforX9)devX9InforMap.get(deviceId);
        devX9InforMapLock.unlock();
        if (dix != null) {
            rd.setCode(0);
            rd.setData(dix.getOnlineFlag());
        } else {
            rd.setMessage("X9控制器未进行初始化，不可进行任何操作");
        }

        rd.setTime(DateFormat.getNowDataFormat());
        return rd;
    }

    public static ReturnData getX9WhiteList() {
        ReturnData rd = new ReturnData();
        rd.setCode(0);
        List<Map> whilteList = new ArrayList();
        devX9InforMapLock.lock();
        Iterator var2 = devX9InforMap.entrySet().iterator();

        while(var2.hasNext()) {
            Map.Entry<String, DeviceInforX9> entry = (Map.Entry)var2.next();
            String deviceId = (String)entry.getKey();
            DeviceInforX9 dix = (DeviceInforX9)entry.getValue();
            Map<String, String> map = new HashMap();
            map.put("deviceId", deviceId);
            map.put("playId", dix.getPlayIds());
            whilteList.add(map);
        }

        devX9InforMapLock.unlock();
        rd.setData(whilteList);
        rd.setTime(DateFormat.getNowDataFormat());
        return rd;
    }

    public static ReturnData delX9DeviceInfor(String deviceId) {
        ReturnData rd = new ReturnData();
        devX9InforMapLock.lock();
        devX9InforMap.remove(deviceId);
        devX9InforMapLock.unlock();
        rd.setCode(0);
        rd.setMessage("删除设备信息成功");
        rd.setTime(DateFormat.getNowDataFormat());
        return rd;
    }

    public static ReturnData clearX9DeviceInfor() {
        ReturnData rd = new ReturnData();
        devX9InforMapLock.lock();
        devX9InforMap.clear();
        devX9InforMapLock.unlock();
        rd.setCode(0);
        rd.setMessage("清空设备信息成功");
        rd.setTime(DateFormat.getNowDataFormat());
        return rd;
    }

    public static ReturnData startX9TcpService(DeviceRealStatus x9RealStatusPush, int port) {
        ReturnData rd = new ReturnData();
        rd.setCode(1);
        if (x9RealStatusPush == null) {
            rd.setMessage("X9设备消息回调类必须实现");
        } else if (port > 1024 && port < 65535) {
            X9ProtocolDeal.x9RealStatusPush = x9RealStatusPush;
            NettyX9ServiceInit nettyX9Serice = new NettyX9ServiceInit(port);
            Thread thread = new Thread(nettyX9Serice);
            thread.start();
            rd.setCode(0);
            rd.setMessage("开启TCP服务端成功");
        } else {
            rd.setMessage("TCP 服务端口范围：1024 - 65535");
        }

        rd.setTime(DateFormat.getNowDataFormat());
        return rd;
    }
}

